from odoo import api, fields, models, _
from odoo.exceptions import UserError


class DUTY_search(models.Model):
    _name = "duty.search"
    _description = "问题疫苗追责"

    duty_id = fields.Char(
        string='susar编号',
        index=True,
        readonly=True,
    )

    @api.model
    def create(self, vals):
        vals['duty_id'] = self.env['ir.sequence'].next_by_code('duty.search') or ''
        return super(DUTY_search, self).create(vals)
    # 疫苗ID
    vaccine_id = fields.Char('疫苗ID', required=True)

    # 疫苗名称

    name = fields.Char(
        string='疫苗中文名',
        required=True
    )
    # 疫苗类别
    vaccine_category = fields.Selection(
        [
            ('1', 'HPV疫苗'),
            ('2', '卡介苗'),
            ('3', '小儿麻痹疫苗'),
            ('4', '狂犬疫苗'),
            ('5', '乙肝疫苗'),
            ('6', '水痘疫苗'),
            ('7', '其他'),
        ],
        string='疫苗类别',
        required=True
    )

    # 单位名称
    organization_name = fields.Char('单位名称', required=True)

    # 生产企业
    manufacturer = fields.Many2one('factory', string='生产企业', required=True)

    # 生产批号
    batch_number = fields.Char('生产批号', required=True)

    # 规格
    standard = fields.Selection(
        [
            ('1', '0.5ml/支'),
            ('2', '1.0ml/支'),
            ('3', '1.5ml/支'),
            ('4', '2.0ml/支'),
            ('5', '2.5ml/支'),
            ('6', '其他'),
        ],
        string='规格',
        required=True
    )

    duty_reason = fields.Html(
        '追责原因',
        required=True
    )
    state = fields.Selection(
        [
            ('edit', '编辑'),
            ('check', '审批中'),
            ('pass', '通过'),
            ('fail', '失败'),
        ],
        string='状态', default='edit', readonly=True, copy=False
    )

    def button_submit(self):
        return self.write({"state": "check"})

    def button_pass(self):
        self.ensure_one()
        # 检验此数据集中是否包含单条数据，否则报错
        sql_query = """SELECT duty_id FROM duty_show where duty_id = '{duty_id}'""".format(
            duty_id=self.duty_id)
        self.env.cr.execute(sql_query)
        result_spe = self.env.cr.dictfetchall()
        if result_spe:
            raise UserError(_('编号%s的追责表已有记录！') % self.duty_id)
        # 有则报错
        else:
            # logger.info("Here is the name: %s", result)
            # check_as_superuser = self.env['duty.show'].sudo()
            # sudo():使用现有数据集创建一个新运行环境，得到一个基于新运行环境的数据集的拷贝
            # check_as_superuser.create({
            #
            # })
            self.write({"state": "pass"})
            self.status = '有效'
            return {
                'type': 'ir.actions.act_window',
                'view_type': 'form',
                'view_mode': 'form,tree',
                'res_model': 'duty.show',
                'target': 'new',
                'context': {
                    'default_duty_id': self.duty_id,
                    'default_vaccine_id': self.vaccine_id,
                    'default_name': self.name,
                    'default_vaccine_category': self.vaccine_category,
                    'default_organization_name': self.organization_name,
                    'default_manufacturer': self.manufacturer.id,
                    'default_batch_number': self.batch_number,
                    'default_standard': self.standard,
                    'default_duty_reason': self.duty_reason,
                }
            }

    def button_fail(self):
        return self.write({"state": "fail"})
